package com.lee.auth.controller;

import com.lee.auth.security.context.AuthContext;
import com.lee.auth.security.context.AuthContextHolder;
import com.lee.common.vo.ResultVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.lee.auth.security.authority.Authority.Admin;
import static com.lee.auth.security.authority.Authority.Root;
import static com.lee.auth.security.context.AuthContextVerifier.*;

/**
 * 一个用于测试的controller
 *
 * @author lee HN
 * @date 2021/4/12 17:07
 */
@Slf4j
@RestController
@RequestMapping("/test")
@AllArgsConstructor
public class TestController {

    @ModelAttribute
    public void model() {
        permitAll();
    }

    @GetMapping("/root")
    public ResultVO<?> root() {
        hasAuthority(Root);
        return ResultVO.success();
    }

    @GetMapping("/admin")
    public ResultVO<?> admin() {
        hasAuthority(Admin);
        return ResultVO.success();
    }

    @GetMapping("/user")
    public ResultVO<?> user() {
        isAuthenticated();
        AuthContext context = AuthContextHolder.getAuthContext();
        return ResultVO.success(context);
    }

}
